import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-04-07
 * Time: 20:43
 */
public class TCPEchoSever {
    //severSocket就是外场拉客的小哥
    //clientSocket就是内场服务的小姐姐
    //severSocket只有一个,clientSocket会给每个客户端都分配一个

    private ServerSocket serverSocket=null;

    public TCPEchoSever(int port) throws IOException {
        serverSocket=new ServerSocket(port);
    }

    public void start() throws IOException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        System.out.println("服务器启动!");
        while(true){

            Socket clientSocket=serverSocket.accept();

                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            processConnection(clientSocket);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });




        }
    }
    //通过这个方法处理一个连接
    //读取请求
    //根据请求计算响应
    //把响应返回给客户端

    private void processConnection(Socket clientSocket) throws IOException {
        System.out.printf("[%s:%d] 客户端上线\n",clientSocket.getInetAddress().toString(),
                clientSocket.getPort());//得到IP和端口号
        //try()这种写法,括号中允许有多个流对象,用;来分割
        try(InputStream inputStream=clientSocket.getInputStream();
            OutputStream outputStream=clientSocket.getOutputStream()) {
            Scanner scanner=new Scanner(inputStream);
            PrintWriter printWriter=new PrintWriter(outputStream);
            while(true){
                //1.读取请求
                if(!scanner.hasNext()){
                    //读取的流到了结尾(对端关闭)
                    System.out.printf("[%s:%d] 客户端下线!\n",clientSocket.getInetAddress(),
                            clientSocket.getPort());
                    break;
                }
                //直接使用scanner读取一段字符串
                String request=scanner.next();
                //2.根据请求响应
                String response=process(request);
                //3.把响应写回到客户端,不要忘了响应也要戴上换行
                printWriter.println(response);
                printWriter.flush();
                System.out.println(response);
                System.out.printf("[%s:%d] req: %s; resp: %s\n",clientSocket.getInetAddress(),
                        clientSocket.getPort(),request,response);
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            clientSocket.close();
        }

    }

    public String process(String request) {
        return request;
    }

    public static void main(String[] args) throws IOException {
        TCPEchoSever tcpEchoSever=new TCPEchoSever(9090);
        tcpEchoSever.start();
    }

}
